package com.kstech.mp3604.data.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.kstech.mp3604.base.Global
import com.kstech.mp3604.bean.Log

/**
 * author: blankii
 * created on: 2025/3/18 14:29
 * description:
 */
@Dao
interface LogDao {
    // 查询
    @Query("SELECT * FROM Log ORDER BY time DESC LIMIT ${Global.LOG_OVERTIME_RETAIN_COUNT}")
    fun queryAllLog(): MutableList<Log>

    //根据姓名参数查询
    @Query("SELECT * FROM Log WHERE DATE(time) = :time")
    fun queryFindLog(time: String): MutableList<Log>

    //查询数据总条数
    @Query("SELECT count(*) FROM Log")
    fun queryAllTaskCountNum(): Int
    // 添加单条数据
    @Insert
    fun addLog(vararg Log: Log)

    // 添加批量数据
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun addBatchLog(list: MutableList<Log>)

    // 更新某一个数据
    @Update
    fun updateLog(vararg Log: Log)

    //更新所有数据
//    @Query("UPDATE loginUser set age='50'")
//    fun updateAll()

    //删除某一个数据
    @Delete
    fun deleteSingle(vararg Log: Log)

    //删除表里所有数据
    @Query("DELETE FROM Log")
    fun deleteAllLog()

    @Query("DELETE FROM Log WHERE id IN (SELECT id FROM  Log ORDER BY time ASC LIMIT ${Global.LOG_OVERTIME_MAX_COUNT - Global.LOG_OVERTIME_RETAIN_COUNT})")
    fun deleteOverTimeData()


    fun checkData() {
        //检查任务表当前存储的数据条数是否达到溢出值
        val currentCount = queryAllTaskCountNum()//当前数据条数
        if (currentCount >= Global.ERROR_OVERTIME_MAX_COUNT) {
            //排好序之后保留固定数据的数据其他全部删除
            deleteOverTimeData()
        }
    }
}